import { writeFileSync } from "node:fs"
import { format } from "prettier"

const EXTENSIONS_SEPARATOR = ","
const MIME_TYPES_SEPARATOR = "_"
const MIME_TYPES_ARRAY_SEPARATOR = "["

const mimes = [
  [["3g2"], "video/3gpp2"],
  [["3gp", "3gpp"], "video/3gpp"],
  [["3mf"], "model/3mf"],
  [["7z"], "application/x-7z-compressed"],
  [["aac"], "audio/aac"],
  [["ac"], "application/pkix-attr-cert"],
  [["adp"], "audio/adpcm"],
  [["adts"], "audio/aac"],
  [["ai"], "application/postscript"],
  [["aml"], "application/automationml-aml+xml"],
  [["amlx"], "application/automationml-amlx+zip"],
  [["amr"], "audio/amr"],
  [["apk"], "application/vnd.android.package-archive"],
  [["apng"], "image/apng"],
  [["appcache", "manifest"], "text/cache-manifest"],
  [["appinstaller"], "application/appinstaller"],
  [["appx"], "application/appx"],
  [["appxbundle"], "application/appxbundle"],
  [["asc"], "application/pgp-keys"],
  [["atom"], "application/atom+xml"],
  [["atomcat"], "application/atomcat+xml"],
  [["atomdeleted"], "application/atomdeleted+xml"],
  [["atomsvc"], "application/atomsvc+xml"],
  [["au", "snd"], "audio/basic"],
  [["avi"], "video/x-msvideo"],
  [["avci"], "image/avci"],
  [["avcs"], "image/avcs"],
  [["avif"], "image/avif"],
  [["aw"], "application/applixware"],
  [["bdoc"], "application/bdoc"],
  [
    [
      "bin",
      "bpk",
      "buffer",
      "deb",
      "deploy",
      "dist",
      "distz",
      "dll",
      "dmg",
      "dms",
      "dump",
      "elc",
      "exe",
      "img",
      "iso",
      "lrf",
      "mar",
      "msi",
      "msm",
      "msp",
      "pkg",
      "so",
    ],
    "application/octet-stream",
  ],
  [["bmp", "dib"], "image/bmp"],
  [["btf", "btif"], "image/prs.btif"],
  [["bz2"], "application/x-bzip2"],
  [["c"], "text/x-c"],
  [["ccxml"], "application/ccxml+xml"],
  [["cdfx"], "application/cdfx+xml"],
  [["cdmia"], "application/cdmi-capability"],
  [["cdmic"], "application/cdmi-container"],
  [["cdmid"], "application/cdmi-domain"],
  [["cdmio"], "application/cdmi-object"],
  [["cdmiq"], "application/cdmi-queue"],
  [["cer"], "application/pkix-cert"],
  [["cgm"], "image/cgm"],
  [["cjs"], "application/node"],
  [["class"], "application/java-vm"],
  [["coffee", "litcoffee"], "text/coffeescript"],
  [["conf", "def", "in", "ini", "list", "log", "text", "txt"], "text/plain"],
  [["cpp", "cxx", "cc"], "text/x-c++src"],
  [["cpl"], "application/cpl+xml"],
  [["cpt"], "application/mac-compactpro"],
  [["crl"], "application/pkix-crl"],
  [["css"], "text/css"],
  [["csv"], "text/csv"],
  [["cu"], "application/cu-seeme"],
  [["cwl"], "application/cwl"],
  [["cww"], "application/prs.cww"],
  [["davmount"], "application/davmount+xml"],
  [["dbk"], "application/docbook+xml"],
  [["doc"], "application/msword"],
  [["docx"], "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
  [["dsc"], "text/prs.lines.tag"],
  [["dssc"], "application/dssc+der"],
  [["dtd"], "application/xml-dtd"],
  [["dwd"], "application/atsc-dwd+xml"],
  [["ear", "jar", "war"], "application/java-archive"],
  [["ecma"], "application/ecmascript"],
  [["emf"], "image/emf"],
  [["eml", "mime"], "message/rfc822"],
  [["emma"], "application/emma+xml"],
  [["emotionml"], "application/emotionml+xml"],
  [["eot"], "application/vnd.ms-fontobject"],
  [["eps", "ps"], "application/postscript"],
  [["epub"], "application/epub+zip"],
  [["exi"], "application/exi"],
  [["exp"], "application/express"],
  [["exr"], "image/aces"],
  [["ez"], "application/andrew-inset"],
  [["fdf"], "application/fdf"],
  [["fdt"], "application/fdt+xml"],
  [["fits"], "image/fits"],
  [["flac"], "audio/flac"],
  [["flv"], "video/x-flv"],
  [["g3"], "image/g3fax"],
  [["geojson"], "application/geo+json"],
  [["gif"], "image/gif"],
  [["glb"], "model/gltf-binary"],
  [["gltf"], "model/gltf+json"],
  [["gml"], "application/gml+xml"],
  [["go"], "text/x-go"],
  [["gpx"], "application/gpx+xml"],
  [["gz"], "application/gzip"],
  [["h"], "text/x-h"],
  [["h261"], "video/h261"],
  [["h263"], "video/h263"],
  [["h264"], "video/h264"],
  [["heic"], "image/heic"],
  [["heics"], "image/heic-sequence"],
  [["heif"], "image/heif"],
  [["heifs"], "image/heif-sequence"],
  [["htm", "html", "shtml"], "text/html"],
  [["ico"], "image/x-icon"],
  [["icns"], "image/x-icns"],
  [["ics", "ifb"], "text/calendar"],
  [["iges", "igs"], "model/iges"],
  [["ink", "inkml"], "application/inkml+xml"],
  [["ipa"], "application/octet-stream"],
  [["java"], "text/x-java-source"],
  [["jp2", "jpg2"], "image/jp2"],
  [["jpeg", "jpe", "jpg"], "image/jpeg"],
  [["jpf", "jpx"], "image/jpx"],
  [["jpm", "jpgm"], "image/jpm"],
  [["jpgv"], "video/jpeg"],
  [["jph"], "image/jph"],
  [["js", "mjs"], "text/javascript"],
  [["json"], "application/json"],
  [["json5"], "application/json5"],
  [["jsonld"], "application/ld+json"],
  [["jsx"], "text/jsx"],
  [["jxl"], "image/jxl"],
  [["jxr"], "image/jxr"],
  [["ktx"], "image/ktx"],
  [["ktx2"], "image/ktx2"],
  [["less"], "text/less"],
  [["m1v", "m2v", "mpe", "mpeg", "mpg"], "video/mpeg"],
  [["m4a"], "audio/mp4"],
  [["m4v"], "video/x-m4v"],
  [["md", "markdown"], "text/markdown"],
  [["mid", "midi", "kar", "rmi"], "audio/midi"],
  [["mkv"], "video/x-matroska"],
  [["mp2", "mp2a", "mp3", "mpga", "m3a", "m2a"], "audio/mpeg"],
  [["mp4", "mp4v", "mpg4"], "video/mp4"],
  [["mp4a"], "audio/mp4"],
  [["mp4s", "m4p"], "application/mp4"],
  [["odp"], "application/vnd.oasis.opendocument.presentation"],
  [["oda"], "application/oda"],
  [["ods"], "application/vnd.oasis.opendocument.spreadsheet"],
  [["odt"], "application/vnd.oasis.opendocument.text"],
  [["oga", "ogg", "opus", "spx"], "audio/ogg"],
  [["ogv"], "video/ogg"],
  [["ogx"], "application/ogg"],
  [["otf"], "font/otf"],
  [["p12", "pfx"], "application/x-pkcs12"],
  [["pdf"], "application/pdf"],
  [["pem"], "application/x-pem-file"],
  [["php"], "text/x-php"],
  [["png"], "image/png"],
  [["ppt"], "application/vnd.ms-powerpoint"],
  [["pptx"], "application/vnd.openxmlformats-officedocument.presentationml.presentation"],
  [["pskcxml"], "application/pskc+xml"],
  [["psd"], "image/vnd.adobe.photoshop"],
  [["py"], "text/x-python"],
  [["qt", "mov"], "video/quicktime"],
  [["rar"], "application/vnd.rar"],
  [["rdf"], "application/rdf+xml"],
  [["rtf"], "text/rtf"],
  [["sass"], "text/x-sass"],
  [["scss"], "text/x-scss"],
  [["sgm", "sgml"], "text/sgml"],
  [["sh"], "application/x-sh"],
  [["svg", "svgz"], "image/svg+xml"],
  [["swf"], "application/x-shockwave-flash"],
  [["tar"], "application/x-tar"],
  [["tif", "tiff"], "image/tiff"],
  [["toml"], "application/toml"],
  [["ts"], "video/mp2t"],
  [["tsx"], "text/tsx"],
  [["tsv"], "text/tab-separated-values"],
  [["ttc"], "font/collection"],
  [["ttf"], "font/ttf"],
  [["vtt"], "text/vtt"],
  [["wasm"], "application/wasm"],
  [["wav"], "audio/wav"],
  [["weba"], "audio/webm"],
  [["webm"], "video/webm"],
  [["webmanifest"], "application/manifest+json"],
  [["webp"], "image/webp"],
  [["wma"], "audio/x-ms-wma"],
  [["wmv"], "video/x-ms-wmv"],
  [["woff"], "font/woff"],
  [["woff2"], "font/woff2"],
  [["xls"], "application/vnd.ms-excel"],
  [["xlsx"], "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
  [["xml"], "application/xml"],
  [["xz"], "application/x-xz"],
  [["yaml", "yml"], "text/yaml"],
  [["zip"], "application/zip"],
]
  .map(([exts, mime]) => {
    return `${exts.join(EXTENSIONS_SEPARATOR)}${MIME_TYPES_SEPARATOR}${mime}`
  })
  .join(MIME_TYPES_ARRAY_SEPARATOR)

format(
  `export const mimeTypes /*#__PURE__*/ = \n'${mimes}'
    
    
export const mimeTypesMap = new Map<string, string>(
  mimeTypes.split("${MIME_TYPES_ARRAY_SEPARATOR}").flatMap((mime) => {
    const [extensions, mimeType] = mime.split("${MIME_TYPES_SEPARATOR}")
    return extensions.split("${EXTENSIONS_SEPARATOR}").map(ext=>[ext, mimeType])
  }),
)
    `,
  {
    parser: "typescript",
    semi: false,
  },
).then((code) => {
  writeFileSync("src/mime-types.ts", code)
})
